<?php
/**
 * 服务窗应用激活开发者模式--应用网关
 * User: zym(wika)
 * Date: 2016/12/1
 * Time: 11:41
 */
class AlipayGatewayAction extends CommonAction
{
    public $configArr;

    public function _initialize()
    {
        $this->configArr = array(
            'alipay_public_key_file' => "/mnt/lost+found/www/wap.alpha.greencampus.cc/uploads/alipay/rsa/common/alipay_rsa_public_key.pem",
            'merchant_private_key_file' => "/mnt/lost+found/www/wap.alpha.greencampus.cc/uploads/alipay/rsa/common/rsa_private_key.pem",
            'merchant_public_key_file' => "/mnt/lost+found/www/wap.alpha.greencampus.cc/uploads/alipay/rsa/common/rsa_public_key.pem",
            /*'alipay_public_key_file' => "/mnt/lost+found/www/pos_pay.xiaotui.so/upload/alipay/alipay_rsa_public_key.pem",
            'merchant_private_key_file' => "/mnt/lost+found/www/pos_pay.xiaotui.so/upload/alipay/rsa_private_key.pem",
            'merchant_public_key_file' => "/mnt/lost+found/www/pos_pay.xiaotui.so/upload/alipay/rsa_public_key.pem",*/
            'charset' => "GBK",
            'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
            'app_id' => "2017042406934384"
        );
    }

    public function index()
    {
        if (get_magic_quotes_gpc()) {
            foreach ($_POST as $key => $value) {
                $_POST [$key] = stripslashes($value);
            }
            foreach ($_GET as $key => $value) {
                $_GET [$key] = stripslashes($value);
            }
            foreach ($_REQUEST as $key => $value) {
                $_REQUEST [$key] = stripslashes($value);
            }
        }
        $sign = $this->_request('sign');
        $sign_type = $this->_request('sign_type');
        $biz_content = $this->_request('biz_content');
        $service = $this->_request('service');
        $charset = $this->_request('charset');
        if (empty ($sign) || empty ($sign_type) || empty ($biz_content) || empty ($service) || empty ($charset)) {
            echo "some parameter is empty.";
            exit();
        }
        // 收到请求，先验证签名
        vendor("Alipay/AlipaySign");
        $as = new AlipaySign();
        $sign_verify = $as->rsaCheckV2($_REQUEST, $this->configArr['alipay_public_key_file']);
        if (!$sign_verify) {
            // 如果验证网关时，请求参数签名失败，则按照标准格式返回，方便在服务窗后台查看。
            if ($this->_request("service") == "alipay.service.check") {
                $this->verifygw(false,$as);
            } else {
                echo "sign verfiy fail.";
            }
            exit();
        }

        // 激活开发者模式
        if ($this->_request("service") == "alipay.service.check") {
            $this->verifygw(true,$as);
        }
    }
    public function verifygw($is_sign_success,$as)
    {
        $biz_content = $this->_request("biz_content");
        $xml = simplexml_load_string($biz_content);
        $EventType = (string)$xml->EventType;
        if ($EventType == "verifygw") {
            if ($is_sign_success) {
                $response_xml = "<success>true</success><biz_content>" . $as->getPublicKeyStr($this->configArr['merchant_public_key_file']) . "</biz_content>";
            } else { // echo $response_xml;
                $response_xml = "<success>false</success><error_code>VERIFY_FAILED</error_code><biz_content>" . $as->getPublicKeyStr($this->configArr['merchant_public_key_file']) . "</biz_content>";
            }
            $return_xml = $as->sign_response($response_xml, $this->configArr['charset'], $this->configArr['merchant_private_key_file']);
            echo $return_xml;
            exit();
        }
    }
}

